package donjuan;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class AutoRegister extends JFrame implements ActionListener
{
	    ArrayList<String> username=null;
	    ArrayList<String> password=null;
	    ArrayList<ArrayList<String>> listname=null;
	    ArrayList<Boolean>del=null;
	    ArrayList<JButton>but=null;
	    String sentinal="#";
	    JPanel upthere;
	    JButton exit;
	    AutoRegister()
	    {
	        username=new ArrayList<String>();
	        password =new ArrayList<String>();
	        listname=new ArrayList<ArrayList<String>>();
	        del=new ArrayList<Boolean>();
	        but=new ArrayList<JButton>();
	        setTitle("auto");
	        setBounds(30,40,700,400);
	        setDefaultCloseOperation(EXIT_ON_CLOSE);
	        setLayout(new FlowLayout());
	        setVisible(true);
	        cindata();
	        init();
	        setbackground();
	        Message.Link_start =true;
	        Message.judgement=true;
	        validate();
	    }
	    private void setbackground()
	    {
	    	  ImageIcon bg=new ImageIcon("src/Autoback.JPG");
	          JLabel label=new JLabel(bg);
	          label.setSize(bg.getIconWidth(),bg.getIconHeight());
	          getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
	          JPanel pan=(JPanel)getContentPane();       //2.把窗口面板设为内容面板并设为透明、流动布局。
	          pan.setOpaque(false);
	          pan.setLayout(new FlowLayout());
	          setSize(bg.getIconWidth(),bg.getIconHeight());  //3.之后把组件和面板添加到窗口面板就可以；
	          setLocationRelativeTo(null);
	    }
	    private void init()
	    {
	        for(int i=0;i< password.size();i++)
	        {
	        	for(int j=0;j<listname.get(i).size();j++) {
	                but.add(new JButton(username.get(i) + ':' + listname.get(i).get(j)));
	                but.get(but.size()-1).setName(username.get(i) + ':' + listname.get(i).get(j));
	                but.get(but.size()-1).addActionListener(this);
	            }
	        }
	        for(JButton temp:but)
	        {
	            temp.setBorder(BorderFactory.createRaisedBevelBorder());
	            temp.setFont(new Font("华文新魏",2,15));
	            temp.setBackground(Color.lightGray);        
	        }
	        for(int i=0;i<but.size();i++)
	        	add(but.get(i));
	        exit=new JButton("退出");
	        exit.setBackground(Color.RED);
	        exit.setFont(new Font("华文新魏",2,15));
	        exit.addActionListener(this);
	        add(exit);
	    }
	  private void cindata()
	   {
	        File cin=null;
	        Reader secret=null;
	        BufferedReader read = null;
	       try{
	           cin=new File("src/autoregister.txt");
	           secret=new FileReader(cin);
	           read= new BufferedReader(secret);
	           String temp;boolean user=false,pass=false;int round=0;
	           while((temp=read.readLine())!=null)
	           {
	                 if(!user)
	                 {
	                     username.add(temp);
	                     user=true;
	                     listname.add(new ArrayList<String>());
	                 }
	                 else if(!pass)
	                 {
	                     password.add(temp);
	                     pass=true;
	                 }
	                 else {
	                     if(!temp.equals(sentinal))
	                        listname.get(round).add(temp);
	                     else {
	                         round++;
	                         pass=user=false;
	                         del.add(true);
	                     }
	                 }
	           }
	           read.close();
	           secret.close();
	       }
	       catch(IOException e) {e.printStackTrace();}
	   }
	   private void refreshdata()
	   {
	       try { // 重新覆盖文件
	           FileWriter fw = new FileWriter("src/autoregister.txt", false);
	           BufferedWriter bw = new BufferedWriter(fw);
	           PrintWriter out = new PrintWriter(bw);
	           for(int i=0;i< password.size();i++)
	           {
	               if(del.get(i))
	               {
	                   out.println(username.get(i));
	                   out.println(password.get(i));
	                   for(int j=0;j< listname.get(i).size();j++)
	                       out.println(listname.get(i).get(j));
	                   out.println(sentinal);
	               }
	               else continue;
	           }
	           out.close();// 关闭文件
	           bw.close();
	           fw.close();
	       } catch (IOException e) { e.printStackTrace(); }
	   }
		public void actionPerformed(ActionEvent e) 
		{
			
			if(e.getSource()==exit)
			{
				 if (Message.con != null)
					 try { Message.con.close();} 
				         catch (SQLException e1) {  System.exit(-1); }  
				  System.exit(0); 
			}
			else 
			{
				for(int i=0;i<but.size();i++)
				{
					if(e.getSource()==but.get(i))
					{
						String []te=but.get(i).getName().split(":");
						Window_ready.contain[0]=te[0];
						Window_ready.contain[2]=te[1];
						for(int k=0;k<password.size();k++)
							if(Window_ready.contain[0].equals(username.get(k))) 
							Window_ready.contain[1]=password.get(k);		
					}
				} 
				Readin.resetdata();
				Message.registersql();
				Message.activatesql();
				Window_main.mode = 6;
			    Window_main.jump();
			    Message.show.setVisible(true);
			    dispose();
			}
		}
}
